/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.agh.student.logic.webapp;

import java.io.Serializable;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;
import pl.edu.agh.student.logic.service.UserServiceImpl;
import pl.edu.agh.student.model.User;

/**
 *
 * @author janburkot
 */
@Stateful
@Name("passwordBean")
@Scope(ScopeType.CONVERSATION)
public class  PasswordBeanImpl implements PasswordBean, Serializable {

    @In(value="userService",create=true,required=false)
    @Out
    protected UserServiceImpl userService;
    @In
    @Out
    Identity identity;
    @Logger
    protected Log log;
    
    @In(required = false)
    @Out(required = false)
    private String oldPassword;
    
    @In(required = false)
    @Out(required = false)
    private String newPassword;
    
    @In(required = false)
    @Out(required = false)
    private String confirm;
    
    public String change(){
        User user = userService.findByUsername(identity.getPrincipal().getName());
        System.out.println(user.getPassword());
        System.out.println(oldPassword);
        System.out.println(newPassword);
        System.out.println(confirm);
        if(!user.getPassword().equals(oldPassword)){
            return "failure";
        }
        
        if("".equals(newPassword) || !newPassword.equals(confirm)){
            return "failure";
        }
        
        user.setPassword(newPassword);
        userService.saveOrUpdate(user);
        return "success";
    }

    @Destroy
    @Remove
    public void destroy() {
        log.debug("destry component #0", this);
    }

}
